var addKeyToTree = function () {
	addNodeToTree('key');
};

var addObjectToTree = function () {
	addNodeToTree('object');
};

var addArrayToTree = function () {
	addNodeToTree('array');
};

var addNodeToTree = function ( type ) {
	var selectionModel = JSONxEdit_jsonTree.getSelectionModel();
	var selectedNode = selectionModel.getSelectedNode();
	var parentNode = null;
	var count = 0;
	var newId = "";
	var selectedId = null;

	if (selectedNode == null)
		selectedNode = JSONxEdit_jsonTree.getRootNode();


	if ( selectedNode.attributes.type == "object" || selectedNode.attributes.type == "array" )
		parentNode = selectedNode;
	else
		parentNode = selectedNode.parentNode;

	if (parentNode.isExpandable() && !parentNode.isExpanded())
		parentNode.expand();

	if ( parentNode.hasChildNodes() )
	{
		parentNode.eachChild(function (child) {
			count++;
		});
	}

	if ( parentNode.attributes.id == "json_tree_root_node" )
	{
		newId = "_1_";
	}
	else
	{
		selectedId = parentNode.attributes.id.split("_");

		var lvl = parseInt( selectedId[ selectedId.length - 2 ] );
		newId = "_" + lvl + "_";
	}

	newId += ""+(count+1);


debug.trace("--"+parentNode.attributes.id);
	if ( !parentNode.hasChildNodes() && parentNode.attributes.id == "json_tree_root_node" )
		parentNode = setTreeDefaultRootNode();

	var newChildConfig = null;
	switch (type)
	{
		case 'key':
			newChildConfig = {
				leaf: true,
				value: "",
				type: "string"
			};
			break;
		case 'object': case 'array':
			newChildConfig = {
				
				leaf: false,
				expandable: true,
				type: type.toLowerCase(),
				iconCls: "ico_" + type.toLowerCase()
			};
			break;
	}

	newChildConfig.id = newId;
	newChildConfig.text = "New" + type.toFirstUpperCase();

	var newChild = new Ext.tree.TreeNode( newChildConfig );

	parentNode.appendChild( newChild );

	if (parentNode.isExpandable() && !parentNode.isExpanded())
		parentNode.expand();

	selectionModel.select( newChild );
};